package com.tanhua.dubbo.server.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * Id自增长
 * 原理：使用Redis的自增长类型，实现mongodb的id自增长
 */
@Service
public class IdService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 生成自增长的id
     */
    public Long createId(String type) {
        type = StringUtils.upperCase(type);
        String key = "TANHUA_ID_" + type;
        Long id = this.redisTemplate.opsForValue().increment(key);
        return id;
    }
}
